<?php 
require_once 'BaseAction.php';
require_once "lib/ThriftLogger.php";

class AdminController extends TTEvent_Base_Action {
    
    public function indexAction(){

        $this->buildAdminMainMenuBlock();
        $req = $this->getRequest();
        $eids = $req->getPost("eids", array());
        if ($req->isPost() && is_array($eids) && count($eids) > 0){
            $action=$req->getPost("command", "");
            
            $eventModel = $this->loadModel('Event');
            if ($action=="delete"){
                $memberModel = $this->loadModel('Member');
                
                // destroy selected events
                foreach($eids as $eid){
                    $eid = (int)$eid;
                    if($eid > 0){
                        $memberModel->removeAllEventRequest($eid);
                        $memberModel->removeAllEventMember($eid);
                        $eventModel->destroyEvent($eid);
                        $this->_helper->modelLoader->getEventInfo($eid, false, false);
                    }
                }
            }
            else{
                $updates = array();
                if ($action == "category"){
                    $new_category = (int)$this->getRequest()->getPost("new_category");
                    if ($new_category > 0)
                        $updates['cid'] = $new_category;
                }
                else if($action == 'promote') $updates['promote'] = 1;
                else if($action == 'unpromote') $updates['promote'] = 0;
                else if($action == 'public') $updates['public'] = 1;
                else if($action == 'unpublic') $updates['public'] = 0;
                
                if(!empty($updates)){
                    $updates['changed'] = time();
                    foreach($eids as $eid){
                        $eid = (int)$eid;
                        if($eid > 0){
                            $eventModel->updateEvent($eid, $updates);
                        }
                    }
                }
            }
            
            $keyword=$req->getPost("keyword", "");
            $public=(int)$req->getPost("public", 0);
            $user_name=$req->getPost("user_name", '');
            $promote=(int)$req->getPost("promote", 0);
            $category=(int)$req->getPost("category", 0);
        }
        else{
            $keyword=$req->getQuery("keyword", "");
            $public=(int)$req->getQuery("public", 0);
            $user_name=$req->getQuery("user_name", '');
            $promote=(int)$req->getQuery("promote", 0);
            $category=(int)$req->getQuery("category", 0);
        }
        
        $ext_param="";
        $search=array();
        if ($keyword!=""){
            $ext_param="keyword=" . urlencode($keyword);
            $search["keyword"]=$keyword;
        }
        if ($public>0){
            $ext_param .= "&public=$public";
            $search["public"] = $public -1;
        }
        if ($promote>0){
            $ext_param .= "&promote=$promote";
            $search["p"] = $promote - 1;
        }
        if ($category>0){
            $ext_param .= "&category=$category";
            $search["cid"] = $category;
        }
        if ($user_name!=""){
            $ext_param .= "&user_name=" . urlencode($user_name);
            //$search["user_name"]=$user_name;
        }
        
        $page=$this->_getParam("page", 0);
        
        $c_model=$this->loadModel("TermData");
        
        $data = $eventModel->getEvents($search, $page);
        $pageInfo = $eventModel->getPageInfo();
        
        $this->view->categoryList = $this->_helper->eventCommon->getEventCategories();
        
        $this->view->search = $search;
        $this->view->events = $data;
        
        $this->view->pager = $this->view->partial("pager.phtml", array("pageInfo" => $pageInfo, 'title'=>'sự kiện', "ext_param" => $ext_param, "url" => "/admin", "title" => "sự kiện"));
    }
    
    private function buildAdminMainMenuBlock($index=0){
        $cache_time = 86400;
        $cache_key = "html_admin_main_menu_".($index>=0?$index:'null');
        $data = $this->getCache($cache_key);
        if(!$data){
            $data=$this->view->partial('admin/adminMainMenu.phtml', array('active_menu'=>$index));
            $this->setCache($cache_key, $data, $cache_time);
        }
        $layout = Zend_Layout::getMvcInstance();
        $layout->menu=$data;
    }
}